package com.appcenter.application.mapper;

import com.appcenter.application.input.orguser.GetOrgUsersInput;
import org.apache.ibatis.annotations.Param;

import static org.apache.http.util.TextUtils.isBlank;

public class OrgUsersProvider {
    public String searchOrgUsers(GetOrgUsersInput input){

        StringBuffer sql = new StringBuffer("select a.* from orgusers a inner join users b on a.UserId=b.Id inner join orgs c on a.orgid=c.id where 1=1  ");
        if(input.getAppCode()==null){
            sql.append(" AND a.AppCode is null");
        }else{
            sql.append(" AND a.AppCode = #{appCode}");
        }

        if(input.getUserId()!=null){
            sql.append(" AND b.Id=#{userId}");
        }

        if(input.getKeyword()!=null){
            sql.append(" AND (b.UserCode like concat('%',#{keyword},'%') or b.UserName like concat('%',#{keyword},'%'))");
        }

        if(input.getOrgId()!=null){
            if(input.getPenetrate()!=null && input.getPenetrate()){
                sql.append(" and (a.OrgId=#{orgId} or a.orgCodePath like concat('%',#{orgCodePath},'%'))");

            }else{
                sql.append(" and a.OrgId=#{orgId}");
            }
        }

        if(input.getKeyword()!=null && !input.getKeyword().equals("")){
            sql.append(" and (b.UserCode like '%#{keyword}%' or b.UserName like '%#{keyword}%' or b.Phone like '%#{keyword}%') ");
        }


        sql.append(" and a.VersionNo=#{versionNo} and b.VersionNo=#{versionNo} and c.VersionNo=#{versionNo} AND (b.AccountIsDisabled is NULL or b.AccountIsDisabled = 0) and a.ValidStatus=1 and c.ValidStatus=1");
        return sql.toString();
    }

    public String GetUserApi(@Param("appCode") String appCode,@Param("orgCode")String orgCode,@Param("roleCode")String roleCode,@Param("jobCode")String jobCode,@Param("userCode")String userCode,@Param("versionNo")Long versionNo){
        StringBuffer sql = new StringBuffer("select a.*,a.id as userId,'"+appCode+"' as appCode,'"+versionNo+"' as versionNo from users a  ");
        if(!isBlank(orgCode)){
            sql.append(" inner join orgusers b on a.id=b.userid and b.ValidStatus=1 and b.VersionNo=#{curversion} and a.VersionNo=#{versionNo} ");
            sql.append(" inner join orgs e on b.orgid=e.id and e.ValidStatus=1 and e.orgcode= #{orgCode} e.VersionNo=#{versionNo} ");
        }
        if(!isBlank(roleCode)){
            sql.append(" inner join roleusers c on a.id=c.userid and c.ValidStatus=1 and a.VersionNo=#{versionNo}");
            sql.append(" inner join roles d on c.roleid=d.id and d.ValidStatus=1 and d.rolecode= #{roleCode}");
            sql.append(" inner join apps f on d.appid=f.id and f.ValidStatus=1 and f.appcode =  #{appCode} ");
        }
        if(!isBlank(jobCode)){
            sql.append(" inner join jobusers j on a.id=j.userid and j.VersionNo=#{versionNo} and a.VersionNo=#{versionNo} and ( a.AccountIsDisabled=0 or a.AccountIsDisabled is NULL ) ");
            sql.append(" inner join jobs b on b.Id=j.JobId and b.JobCode=#{jobCode} and b.ValidStatus=1 and b.VersionNo=#{versionNo} ");
            if (!isBlank(orgCode)) {
                sql.append(" inner join orgjobs oj on orgjobs.JobId=b.Id  and oj.VersionNo=#{versionNo}");
                sql.append(" inner join orgs  o on o.Id=oj.OrgId");
                sql.append(" and o.ValidStatus=1 and o.VersionNo=#{versionNo}");
            }

        }
        sql.append(" where a.VersionNo= #{versionNo}");
        if(!isBlank(userCode)){
            sql.append(" and a.usercode= #{userCode}");
        }
        //sql.append(" limit 10");
        return sql.toString();
    }
}
